package top.aiprogram.user.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import top.aiprogram.user.entity.Student;
import top.aiprogram.user.service.StudentService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


//从前端把值传到SpringMVC中
//get:从前台获取数据   post：提交数据
//请求转发(forword)、重定向(redirect)

@Controller     //请求处理器注解
@RequestMapping("user")     //请求路径只要带有user，就把请求分发到UserController里面去处理
public class UserController {   //把这个类作为一个处理器

    //每个请求对应一个方法

    //从后端把数据放到前台常见的两种方式：1、ModelAndView   2、ModelMap
//    @RequestMapping("/test")    //方法请求路径
//    public ModelAndView testUser(){
//        ModelAndView model = new ModelAndView();
//        model.setViewName("user");
//        User user = new User();
//        user.setId(28);
//        user.setName("laowang");
//        model.addObject("user",user);
//        return model;
//    }


    //get请求方法进行参数传递
//    @RequestMapping("/test/{id}/{name}")
//    public String testUser(ModelMap modelMap,@PathVariable("id") Integer sid,@PathVariable("name") String sname){

    //post传递两种方式：1、定义HttpServletRequest     2、直接new对象

//    @RequestMapping("/test")
//    public String testUser(ModelMap modelMap, HttpServletRequest request) {
//    public String testUser(ModelMap modelMap,User user) {

//        String id = request.getParameter("id");
//        String name = request.getParameter("name");
//        User user = new User();
//        user.setId(29);
//        user.setName("laoli");
//        modelMap.addAttribute("user", user);
//        return "redirect:/user/demo";
//
//    }

    //对象转json输出，前端接收json
//    @RequestMapping("/test")
//    @ResponseBody
//    public User textUser(){
//        User user = new User();
//        user.setId(28);
//        user.setName("laowang");
//        return user;
//    }

    //前端传入json
//    @RequestMapping(value = "test",method = RequestMethod.POST)
    @Autowired
    private StudentService studentService;
    @RequestMapping(value = "test")
    public String testUser(@RequestBody Student student){

        //Controller层调用Service层，然后Service层调用dao层
        //Controller接收参数，调用相应的业务

        studentService.createStudent(student);



        return "user";
    }

    @RequestMapping("/demo")
    public String demoUser() {
        return "demo";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file,HttpServletRequest request) throws IOException {
        String name = file.getOriginalFilename();
        File uploadF = new File("/"+name);
        file.transferTo(uploadF);
        return "demo";
    }

    @RequestMapping("/down")
    public ResponseEntity<byte[]> download() throws IOException {
        String path = "E:/1.png";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
//        attachment：附件
        headers.setContentDispositionFormData("attachment",path);
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }


}
